package com.example.score.controllrt;

import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.example.score.common.AjaxResult;
import com.example.score.entity.TUser;
import com.example.score.entity.Teaclassuser;
import com.example.score.entity.vo.TUserVo;
import com.example.score.service.TeaclassuserService;
import com.example.score.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class Usercontroller {
    @Autowired
    private UserService userService;
    @Autowired
    TeaclassuserService teaclassuserService;

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public AjaxResult login(String username, String password, HttpServletRequest request) {
        // 判断参数
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return AjaxResult.fail(-1, "参数有误11");
        }
        // 根据用户名获取user对象
        TUser user = userService.getTUserByUsername(username);
        if (user == null) {
            return AjaxResult.fail(-1, "参数有误22");
        }
        // 验证并把user对象保存到session
        if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
            user.setPassword("");
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
//            System.out.println(user);
            return AjaxResult.success(user);
        }
        return AjaxResult.fail(-2, "用户名或密码错误");
    }

    /**
     * 获取用户列表
     * @return
     */
    @RequestMapping("/list")
    public AjaxResult getList(Integer classId) {
        // 验证参数
        if (classId == null || classId <= 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        // 获取用户列表
        List<TUser> list = userService.getList();
        if (list == null || list.size() <= 0) {
            return AjaxResult.fail(-2, "获取用户列表失败");
        }
        for (TUser user : list) {
            user.setPassword("");
        }
        // 根据classId获取已经存在的userId
        List<Integer> userList = teaclassuserService.selectExits(classId);
        Map<String, Object> map = new HashMap<>();
        map.put("user_id_list", userList);
        map.put("user_list", list);
        return AjaxResult.success(map);
    }


    @RequestMapping("/Select")
    public AjaxResult Select(Integer nj,Integer zy, Integer bj) {
        // 验证参数
//        if ( nj <= 0 || zy <=0 || bj <=0) {
//            return AjaxResult.fail(-1, "参数错误");
//        }
        // 获取用户列表
        List<TUser> list = userService.getList();
        if (list == null || list.size() <= 0) {
            return AjaxResult.fail(-2, "获取用户列表失败");
        }
        for (TUser user : list) {
            user.setPassword("");
        }
        // 根据classId获取已经存在的userId
        List<TUserVo> userList = teaclassuserService.selectAttribute(nj,zy,bj);
        return AjaxResult.success(userList);
    }



    /**
     * 根据Excel导入
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/importStudentbyex")
    public AjaxResult importStudentByExcel(MultipartFile file) throws IOException {
        if (file == null) {
            return AjaxResult.fail(-1, "参数错误");
        }
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List<TUser> users = reader.readAll(TUser.class);

        try {
            int inserts = userService.inserts(users);
            if (inserts < 0) {
                return AjaxResult.fail(-2, "插入失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
            int insertsauthority = userService.insertsauthority(users);
            if (insertsauthority < 0) {
                return AjaxResult.fail(-2, "插入失败");
            }
            return AjaxResult.success("插入成功");
        };


    @RequestMapping("/register")
    public AjaxResult register(@RequestBody TUser list) {
        // 验证参数
        if ( list == null) {
            return AjaxResult.fail(-1, "参数错误");
        }
        // 获取用户列表
        Integer regist = userService.register(list);
        if (regist == null ) {
            return AjaxResult.fail(-2, "注册失败");
        }
        return AjaxResult.success("注册成功");
    }
    @RequestMapping("/logout")
    public AjaxResult logout(@RequestParam("list") List<Integer> list) {
        // 验证参数
        if ( list == null) {
            return AjaxResult.fail(-1, "参数错误");
        }
        // 获取用户列表
        Integer regist = userService.logout(list);
        if (regist == null ) {
            return AjaxResult.fail(-2, "注册失败");
        }
        return AjaxResult.success("注销成功");
    }

    @RequestMapping("/loginout")
    public AjaxResult logout(HttpServletRequest request) {
        HttpSession session = request.getSession(false); // 获取现有会话，如果不存在则返回 null
        if (session != null) {
            session.invalidate(); // 使会话失效
            return AjaxResult.success("成功退出登录");
        } else {
            return AjaxResult.fail(-1, "未登录");
        }
    }
    }
